home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c,comp.unix.programmer
- Subject: Re: Q: '\n' character
- Date: 15 Apr 1996 07:37:56 -0700
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4ktn04INNoev@keats.ugrad.cs.ubc.ca>
- References: <4kj66f$k0o@ren.cei.net> <4kmdsv$ojc@masala.cc.uh.edu> <4kmhpsINN7ak@keats.ugrad.cs.ubc.ca> <AD97189A966891F2@mcdiala02.it.luc.edu>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <AD97189A966891F2@mcdiala02.it.luc.edu>,
- Verne Arase <VArase@varase.it.luc.edu> wrote:
- >In article <4kmhpsINN7ak@keats.ugrad.cs.ubc.ca>,
- >c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) wrote:
- >
- > >Humor me as to why it is necessary to retain a whole string of data just
- >to
- > >then scan through it to find its length to remove the trailining newline,
- > >which was put there by a standard library function that already scanned
- >the
- > >characters once to _find_ the newline in the first place.
- >
- >This is a failing of the C standard I/O library; fgets() _ought_ to have
- >returned the length read.
-
- Yes it should. A lot of the standard I/O library is braindead, but it had to be
- standardized a certain way to reflect existing practice and preserve the
- correctness of existing programs.
-
- Having a return value that is the same as one of the arguments is a complete
- waste of a ``data path'' that could be used to return pertinent information.
-
- Or better still: how about returning a pointer to the last character read! This
- would point to a null character if _no_ characters were read (thus indicating
- EOF on the attempt to read the first character), or to either a newline or a
- non-newline if at least one character was read. It would always be valid to
- dereference the returned pointer, provided a valid buffer was passed in. A use
- of fgets would then look like:
-
-
- switch(*(ptr = nfgets(buf, sizeof buf, stdin))) {
- case '\0':
- /* EOF encountered */
- break;
- case '\n':
- /* newline-terminated line read */
- len = ptr - buf;
- *ptr = '\0';
- break;
- default:
- len = ptr - buf + 1;
- /* incomplete line read or line incompletely read */
- break;
- }
-
- --
- I'm not really a jerk, but I play one on Usenet.
-